import Vue from 'vue'
import Router from 'vue-router'
import approvalsRouter from './modules/approvals'// 审批
import departmentsRouter from './modules/departments'// 组织架构
import employeesRouter from './modules/employees'// 员工
import permissionRouter from './modules/permission'// 权限管理
import attendancesRouter from './modules/attendances'// 考勤
import salarysRouter from './modules/salarys'// 工资
import settingRouter from './modules/setting'// 公司设置
import socialRouter from './modules/social'// 社保
Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  {
    path: '/excel',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        component: () => import('@/views/excel')

      }
    ]
  }

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

// 动态路由
export const asyncRoutes = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  permissionRouter,
  approvalsRouter, // 本期不实现功能
  attendancesRouter, // 本期不实现功能
  salarysRouter, // 本期不实现功能
  socialRouter // 本期不实现功能
]

const createRouter = () => new Router({
  // 控制路由滚动行为  滚动到顶部
  scrollBehavior: () => ({ y: 0 }),
  // 组合到一起组成路由表
  // routes: [...constantRoutes, ...asyncRoutes]
  routes: [...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由
// 将路由重新实例化，相当于换了一个新的路由，之前**`加的路由`**就不存在了
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router 重新设置路由的可匹配路径
}

export default router
